<?php

class Search
{
	 public static $criteria;
	 	 
	  public static function getLiftersCount($p_criteria)
	 {
		 return SQL::num_rows(Template::render("findLifter.sql", "sql", array('criteria'=>$p_criteria)));		  
	 }
	 
	 public static function getLiftersFriendsCount()
	 {
		 return SQL::num_rows(Template::render("getMyFriends.sql", "sql", array('my_id'=>$_SESSION['user_id'])));		  
	 }
	 
	 public static function getAdvertsCount($p_criteria)
	 {
		 return SQL::num_rows(Template::render("findAdverts.sql", "sql", array('criteria'=>$p_criteria)));		  
	 }
	 
	 public static function getLifters($p_criteria, $p_start_page)
	 {
		 if ($p_start_page==1)
		 {
			 $criteria=$p_criteria.' LIMIT 10';
		 }
		 else
		 {
			 $page=($p_start_page-1)*10;
			 $criteria=$p_criteria." LIMIT $page, 10";
		 }
		 
		 $data=SQL::select(Template::render("findLifter.sql", "sql", array('criteria'=>$criteria)));
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }
	 }
	 
	 public static function getLiftersFriends($p_start_page)
	 {
		 $query=Template::render("getMyFriends.sql", "sql", array('my_id'=>$_SESSION['user_id']));
		 
		 if ($p_start_page==1)
		 {
			 $query=$query.' LIMIT 10';
		 }
		 else
		 {
			 $page=($p_start_page-1)*10;
			 $query=$$query." LIMIT $page, 10";
		 }
		 
		 $data=SQL::select($query);	
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }
	 }
	 
	  public static function getAdverts($p_criteria, $p_start_page)
	 {
		 if ($p_start_page==1)
		 {
			 $criteria=$p_criteria.' ORDER BY adverts.date DESC LIMIT 10 ';
		 }
		 else
		 {
			 $page=($p_start_page-1)*10;
			 $criteria=$p_criteria." ORDER BY adverts.date DESC LIMIT $page, 10 ";
		 }
		 
		 $data=SQL::select(Template::render("findAdverts.sql", "sql", array('criteria'=>$criteria)));
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }
	 }
	 
	  public static function createQuery($p_data)
	 {
		  self::$criteria="";
		  if (!empty($p_data['name']))
		 {
			 $first_last_name="";
			 $first_last_name=explode(" ", $p_data['name']);
			 if (empty($first_last_name[1])) 
			 {
				 self::$criteria="users.first_name='".$first_last_name[0]."' OR users.last_name='".$first_last_name[0]."' ";
			 }			 
			 else
			 {
				 self::$criteria="users.first_name='".$first_last_name[0]."' AND users.last_name='".$first_last_name[1]."' ";
			 }
		 }
		 
		 if ($p_data['set_date_of_birth'])
		 {
			 $date=$p_data['set_date_of_birth_year'].'-'.$p_data['set_date_of_birth_month'].'-'.$p_data['set_date_of_birth_day'];
			 if (!empty(self::$criteria))
			 {
				 self::$criteria=self::$criteria."AND (users.date_of_birth='".$date."') ";
			 }
			 else
			 {
				 self::$criteria=self::$criteria."users.date_of_birth='".$date."' ";
			 }			 
		 }
		 else
		 {
			 $cur_date = getdate();		   
			 $min_year=$cur_date['year']-$p_data['age_from'];
			 $max_year=$cur_date['year']-$p_data['age_to'];
			 if (!empty(self::$criteria))
			 {
				 self::$criteria=self::$criteria."AND (users.date_of_birth IS NULL OR (YEAR(users.date_of_birth) BETWEEN '$max_year' AND '$min_year')) ";
			 }
			 else
			 {
				 self::$criteria=self::$criteria."(users.date_of_birth IS NULL OR (YEAR(users.date_of_birth) BETWEEN '$max_year' AND '$min_year')) ";
			 }
		 }		 
		 
		 if ($p_data['country']!='n')
		 {
			 self::$criteria=self::$criteria."AND (country.id=".$p_data['country'].") ";
		 }
		  if ($p_data['region']!='n')
		 {
			 self::$criteria=self::$criteria."AND (region.id=".$p_data['region'].") ";
		 }
		  if ($p_data['city']!='n')
		 {
			 self::$criteria=self::$criteria."AND (city.id=".$p_data['city'].") ";
		 }
		 if ($p_data['set_power_data'])
		 {
			 self::$criteria=self::$criteria."AND (users.squat BETWEEN ".$p_data['squat_from']." AND ".$p_data['squat_to'].") ";
			 self::$criteria=self::$criteria."AND (users.benchpress BETWEEN ".$p_data['press_from']." AND ".$p_data['press_to'].") ";
			 self::$criteria=self::$criteria."AND (users.deadlift BETWEEN ".$p_data['dlift_from']." AND ".$p_data['dlift_to'].") ";
			 self::$criteria=self::$criteria."AND ((users.squat+users.benchpress+users.deadlift) BETWEEN ".$p_data['total_from']." AND ".$p_data['total_to'].") ";
		 }
		 if ($p_data['set_antropometr_data'])
		 {
			 self::$criteria=self::$criteria."AND (users.height BETWEEN ".$p_data['height_from']." AND ".$p_data['height_to'].") ";
			 self::$criteria=self::$criteria."AND (users.weight BETWEEN ".$p_data['weight_from']." AND ".$p_data['weight_to'].") ";
		 }
		 if ($p_data['set_federation'])
		 {
			 $federation_criteria=array();
			 $is_selected=0;
			 
			 if ($p_data['ipf'])
			 {
				 $federation_criteria['ipf']="users.federation LIKE '%\"ipf\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['wpc'])
			 {
				 $federation_criteria['wpc']="users.federation LIKE '%\"wpc\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['wpo'])
			 {
				 $federation_criteria['wpo']="users.federation LIKE '%\"wpo\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['gpc'])
			 {
				 $federation_criteria['gpc']="users.federation LIKE '%\"gpc\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['wdfpf'])
			 {
				 $federation_criteria['wdfpf']="users.federation LIKE '%\"wdfpf\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['awpc'])
			 {
				 $federation_criteria['awpc']="users.federation LIKE '%\"awpc\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['ipa'])
			 {
				 $federation_criteria['ipa']="users.federation LIKE '%\"ipa\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['gpu'])
			 {
				 $federation_criteria['gpu']="users.federation LIKE '%\"gpu\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['udfpf'])
			 {
				 $federation_criteria['udfpf']="users.federation LIKE '%\"udfpf\";b:1%'";
				 $is_selected=1;
			 }
			 if ($p_data['raw'])
			 {
				 $federation_criteria['raw']="users.federation LIKE '%\"raw\";b:1%'";
				 $is_selected=1;
			 }
			 if ($is_selected)
			 {
				 self::$criteria=self::$criteria."AND (";
				 $total_fed_criter;
				 foreach($federation_criteria as $k=>$v)
				 {
					 if (!empty($total_fed_criter))
					 {
						 $total_fed_criter=$total_fed_criter.' OR '.$v;
					 }
					 else
					 {
						 $total_fed_criter=$v;
					 }
					 
				 }
				 
				 self::$criteria=self::$criteria.$total_fed_criter;
				 self::$criteria=self::$criteria.")";				 
			 }			 
			
		 }
		  if ($p_data['set_tityl'])
		 {
			 $tityl_criteria=array();
			 $is_selected=0;
			 
			 if ($p_data['rate_3jun'])
			 {
				 $tityl_criteria['rate_3jun']="users.level = '3jun'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_2jun'])
			 {
				 $tityl_criteria['rate_2jun']="users.level = '2jun'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_1jun'])
			 {
				 $tityl_criteria['rate_1jun']="users.level = '1jun'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_3man'])
			 {
				 $tityl_criteria['rate_3man']="users.level = '3man'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_2man'])
			 {
				 $tityl_criteria['rate_2man']="users.level = '2man'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_1man'])
			 {
				 $tityl_criteria['rate_1man']="users.level = '1man'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_kms'])
			 {
				 $tityl_criteria['rate_kms']="users.level = 'kms'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_ms'])
			 {
				 $tityl_criteria['rate_ms']="users.level = 'ms'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_msmk'])
			 {
				 $tityl_criteria['rate_msmk']="users.level = 'msmk'";
				 $is_selected=1;
			 }
			 if ($p_data['rate_zms'])
			 {
				 $tityl_criteria['rate_zmk']="users.level = 'zms'";;
				 $is_selected=1;
			 }
			 if ($is_selected)
			 {
				 self::$criteria=self::$criteria."AND (";
				 $total_tyt_criter;
				 foreach($tityl_criteria as $k=>$v)
				 {
					 if (!empty($total_fed_criter))
					 {
						 $total_tyt_criter=$total_fed_criter.' OR '.$v;
					 }
					 else
					 {
						 $total_tyt_criter=$v;
					 }
					 
				 }
				  self::$criteria=self::$criteria.$total_tyt_criter;
				 self::$criteria=self::$criteria.")";
			 }
		 }
		 return self::$criteria;
	 }
	 
	 public static function createAdvertQuery($p_data)
	 {
		 self::$criteria="";
		 $rubric_criteria=array();
		 $categ_criteria=array();
		 $ekip_type_criteria=array();
		 if ($p_data['rubric_buy'])
		 {
			 $rubric_criteria[]="adverts.rubric='buy'";
		 }
		 if ($p_data['rubric_sell'])
		 {
			 $rubric_criteria[]="adverts.rubric='sell'";
		 }
		 if ($p_data['rubric_change'])
		 {
			 $rubric_criteria[]="adverts.rubric='change'";
		 }
		  if ($p_data['categ_ekipa'])
		 {
			 $categ_criteria[]="adverts.category='ekipa'";
		 }
		 if ($p_data['categ_sportpit'])
		 {
			  $categ_criteria[]="adverts.category='sportpit'";
		 }
		 if ($p_data['categ_hardware'])
		 {
			 $categ_criteria[]="adverts.category='equipment'";
		 }
		  if ($p_data['categ_other'])
		 {
			  $categ_criteria[]="adverts.rubric='other'";
		 }
		  if ($p_data['ekypa_type_shirt'])
		 {
			 $ekip_type_criteria[]="adverts.ekip_type='shirt'";
		 }
		 if ($p_data['ekypa_type_kombez'])
		 {
			 $ekip_type_criteria[]="adverts.ekip_type='kombez'";
		 }
		 if ($p_data['ekypa_type_wraps'])
		 {
			 $ekip_type_criteria[]="adverts.ekip_type='wraps'";
		 }
		  if ($p_data['ekypa_type_belt'])
		 {
			 $ekip_type_criteria[]="adverts.ekip_type='belt'";
		 }
		  if ($p_data['ekypa_type_boots'])
		 {
			$ekip_type_criteria[]="adverts.ekip_type='boots'";
		 }
		 
		 if (!empty($rubric_criteria))
		 {
			 self::$criteria=self::$criteria."(";
			 $rub;
			 foreach ($rubric_criteria as $k=>$v)
			 {
				 if (empty($rub))
				 {
					 $rub=$v;
				 }
				 else
				 {
					 $rub=$rub." OR ".$v;
				 }
			 }	
			self::$criteria=self::$criteria.$rub.") ";	
		 }
		 
		 if (!empty($categ_criteria))
		 {
			 if (!empty(self::$criteria))
			 {
				 self::$criteria=self::$criteria."AND (";
			 }
			 else
			 {
				 self::$criteria=self::$criteria."(";
			 }
			 $cat;
			 foreach ($categ_criteria as $k=>$v)
			 {
				 if (empty($cat))
				 {
					 $cat=$v;
				 }
				 else
				 {
					 $cat=$cat." OR ".$v;
				 }
				 if ($v=="adverts.category='ekipa'")
				 {
					 if (!empty($ekip_type_criteria))
					{
						 $cat=$cat." AND (";
						 $ekip_type;
						 foreach ($ekip_type_criteria as $kk=>$vv)
						 {
							 if (empty($ekip_type))
							 {
								 $ekip_type=$vv;
							 }
							 else
							 {
								 $ekip_type=$ekip_type." OR ".$vv;
							 }
						 }	
						$cat=$cat.$ekip_type.")";
					}	
				 }
			 }	
			self::$criteria=self::$criteria.$cat.") ";	
		 }			
		 		 
		 if (empty(self::$criteria))
		 {
			 self::$criteria="adverts.pu_users_id=".$_SESSION['user_id'];
		 }
		 return self::$criteria;
	 }
	 
	 public static function getNewsCount($p_filter)
	 {
		 $query=Template::render("News.sql", "sql", array());
		 $criteria=array();
		 
		 if (!$p_filter['advert_checked'])
		 {
			 $criteria[]="news_type!='advert'";
		 }
		 if (!$p_filter['photo_checked'])
		 {
			 $criteria[]="news_type!='photo'";
		 }
		 if (!$p_filter['video_checked'])
		 {
			 $criteria[]="news_type!='video'";
		 }
		 
		if (!empty($criteria))
		 {
			 $filter;
			 foreach ($criteria as $k=>$v)
			 {
				 if (empty($filter))
				 {
					 $filter=$v;
				 }
				 else
				 {
					 $filter=$filter." AND ".$v;
				 }
			 }
			 $filter=" WHERE (".$filter;
			 $query=$query.$filter.") ";		  
		 }		
		 
		 return SQL::num_rows($query);
	}  
	  
	  
	  public static function getNews($p_filter, $p_start_page)
	  {
		 $query=Template::render("news.sql", "sql", array());
		 $criteria=array(); 	
		 
		 if (!$p_filter['advert_checked'])
		 {
			 $criteria[]="news_type!='advert'";
		 }
		 if (!$p_filter['photo_checked'])
		 {
			 $criteria[]="news_type!='photo'";
		 }
		 if (!$p_filter['video_checked'])
		 {
			 $criteria[]="news_type!='video'";
		 }
		 
		 if (!empty($criteria))
		 {
			 $filter;
			 foreach ($criteria as $k=>$v)
			 {
				 if (empty($filter))
				 {
					 $filter=$v;
				 }
				 else
				 {
					 $filter=$filter." AND ".$v;
				 }
			 }
			 $filter=" WHERE (".$filter;
			 $query=$query.$filter.") ";		  
		 }		 
		 
		 if ($p_start_page==1)
		 {
			 $query=$query.' LIMIT 10';
		 }
		 else
		 {
			 $page=($p_start_page-1)*10;
			 $query=$query." LIMIT $page, 10";
		 }
		
		 $data=SQL::select($query);
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }
	  }
}